package com.youhong.teethcare.utils;

import android.content.Context;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;
import android.widget.Toast;

import com.youhong.teethcare.R;

/**
 * Created by Jimmy on 2016/8/30 0030.
 */
public class ToastUtil {

    private static Toast mToast;

    public static void showShortToast(Context context, String text) {
        showToast(context, text, Toast.LENGTH_SHORT);
    }

    public static void showShortToast(Context context, int resId) {
        showToast(context, context.getResources().getString(resId), Toast.LENGTH_SHORT);

    }

    public static void showLongToast(Context context, String text) {
        showToast(context, text, Toast.LENGTH_LONG);

    }

    private static void showToast(Context context, String text, int short_long) {
        WindowManager wm = (WindowManager) context
                .getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics outMetrics = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(outMetrics);

        int height = outMetrics.heightPixels;

        if (mToast != null) {
            mToast.cancel();
        }
        mToast = new Toast(context);
        View v = View.inflate(context, R.layout.customer_toast, null);

        //设置文本
        ((TextView) v.findViewById(R.id.customer_toast_tv)).setText(text);
        mToast.setView(v);
        // 这里给了一个1/4屏幕高度的y轴偏移量
        mToast.setGravity(Gravity.TOP, 0, height / 4);
        mToast.show();
    }

    public static void showLongToast(Context context, int resId) {
        showToast(context, context.getResources().getString(resId), Toast.LENGTH_LONG);
    }
}
